package com.imooc.reader.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.imooc.reader.entity.*;
import com.imooc.reader.service.BookService;
import com.imooc.reader.service.CategoryService;
import com.imooc.reader.service.EvaluationService;
import com.imooc.reader.service.MemberReadStateService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
public class BookController {
    @Resource
    private CategoryService categoryService;

    @Resource
    private BookService bookService;

    @Resource
    private EvaluationService evaluationService;

    @Resource
    private MemberReadStateService memberReadStateService;
    /**
     * 首页
     * @return
     */
    @GetMapping("/")
    public ModelAndView showIndex() {
        ModelAndView modelAndView = new ModelAndView("/index");
        List<Category> categoryList = categoryService.selectAll();
        modelAndView.addObject("categoryList", categoryList);
        return modelAndView;
    }

    /**
     * 分页查询
     * @param categoryId 分类id
     * @param order 排序
     * @param p 当前页数
     * @return
     */
    @RequestMapping("/findPage")
    @ResponseBody
    public IPage<Book> findPage(Long categoryId,String order,Integer p){
        IPage<Book> paging = bookService.paging(categoryId,order,p, 10);
        return paging;
    }

    @RequestMapping("/books/{id}")
    public ModelAndView showDetile(@PathVariable("id") Long bookId, HttpServletRequest request){
        /*从session中获取会员信息*/
        Member member = (Member) request.getSession().getAttribute("checkLogin");

        ModelAndView modelAndView=new ModelAndView("/detail");

        Book book = bookService.findBookById(bookId);
        List<Evaluation> evaluation = evaluationService.findEvaluation(bookId);

        //用户已经登录
        if(member!=null){
            MemberReadState memberReadState = memberReadStateService.selectState(member.getMemberId(), bookId);
            modelAndView.addObject("memberReadState",memberReadState);
        }
        modelAndView.addObject("book",book);
        modelAndView.addObject("evaluation",evaluation);

        return modelAndView;
    }

    @GetMapping("/debug/evaluation/{id}")
    @ResponseBody
    public Evaluation debugEvaluation(@PathVariable Long id) {
        Evaluation eva = evaluationService.findEvaluation(id).get(1);
//        logger.info("调试输出 - ID类型: {}", eva.getId().getClass());
        System.out.println(eva.getEvaluationId().getClass());
        System.out.println("  ID 值: " + eva.getEvaluationId());
        return eva; // 返回原始JSON
    }

    @GetMapping("/books")
    @ResponseBody
    public IPage<Book> selectPage(Integer page, Integer limit){
        if(page == null || page < 1){
            page = 1;
        }
        if(limit == null || limit < 1){
            limit = 10;
        }
        IPage<Book> pageObeject = bookService.paging2(page, limit);
        return pageObeject;
    }
}
